	PROGRAM HISTFRCST
	INTEGER ONE,TWO,THREE,FOUR,SIX,POS,START,END
	CHARACTER*2 WY
	CHARACTER*4 YEAR
	CHARACTER*3 MON,ENDMONTH
	CHARACTER*9 ADATA(5)
	CHARACTER*10 VARIABLES,CHANGES
	CHARACTER*20 TITMAS,TITLE,TIT
	CHARACTER*114 A(10000),B
	DIMENSION DATA(5),RDATA(5),ERROR(10)
C
C
C
C	UNIT=1 IS ADD.OUT OR HISTORY FILE
C	UNIT=2 IS A ROCOMP FILE
C	UNIT=3 IS OUTPUT FLE
C	UNIT=4 IS FRSTSUM.DAT
C	UNIT=6 IS ERROR CHECKING FILE
	FOUR=4
	ONE=1
	TWO=2
	THREE=3
	SIX=9
	OPEN(UNIT=SIX,CARRIAGECONTROL='LIST',STATUS='UNKNOWN')
C
C
	IB=100000
	CHANGES='          '
	VARIABLES='          '
	I=1
505	READ(ONE,1001,END=200)A(I)  ! HISTORICAL REGRESSION FILE
1001	FORMAT(A)
	I=I+1
	GO TO 505
200	NREC=I
C
	WRITE(6,1013)
1013	FORMAT(' ENTER VARIABLE CHARACTER STRING FOR EACH VARIABLE',/,
	1      ' TO BE REPLACED.  ENTER ONLY ONE CHARACTER FOR EACH',/,
	1      ' VARIABLE.  FOR EXAMPLE "ASPN" WOULD REPLACE FOUR',/,
	1      ' VARIABLE TYPES.  IF MORE THAN ONE VARIABLE IS A TYPE',/,
	1      ' THOSE VARIABLES WILL ALSO BE CHANGED.',/, 
	1      '                                                 ',/,
	1      ' A= ANTECEDENT RUNOFF',/,
	1      ' S= SNOW',/,
	1      ' P= PRECIPITATION',/,
	1      ' N= NATURAL RUNOFF',/,
	1      ' C= COMBINATION PRECIP AND SNOW',//,
	1      ' ENTER STRING >',$)
	READ(5,1014)NCHRS,(CHANGES(N:N),N=1,NCHRS)
1014	FORMAT(Q,10A1)
	POS=INDEX(CHANGES(1:),' ')
C
C	
	WRITE(6,1016)
1016	FORMAT(' ENTER STARTING YEAR >',$)
	READ(5,1017)ISY
1017	FORMAT(I4)
	WRITE(6,1018)
1018	FORMAT(' ENTER ENDING YEAR >',$)
	READ(5,1017)IEY
	ISY=ISY-1900
	IEY=IEY-1900
C
C
C
C
C
C
	DO 400 IY=ISY,IEY
C
	ENCODE(2,1010,WY)IY
1010	FORMAT(I2)
C
C
C
503	READ(FOUR,1000,END=502)TITMAS,ENDMONTH,VARIABLES !MASTER TITLE DATA FILE
1000	FORMAT(A,10X,A,21X,A)
C	WRITE(SIX,1019)TITMAS,ENDMONTH,VARIABLES
C1019	FORMAT(' TITMAS= ',A,5X,'ENDMONTH= ',A,5X,'VARABILES= ',A,
C	1      ' %WRITE(SIX,1019)' )
C
	OPEN(UNIT=TWO,FILE='ROCOMP'//ENDMONTH//'.'//WY,ERR=9000,
	1STATUS='UNKNOWN')
C	WRITE(SIX,1022)ENDMONTH,WY,TITMAS,VARIABLES
C1022	FORMAT(1X,/,1X,'OPENED FILE= ROCOMP',A,'.',A,
C	13X,'TITMAS= ',A,5X,'VARIABLES= ',A)
C	OPEN FINAL VARIABLE FILE CREATED FORM THE FORECAST PROGRAM
C
504	READ(TWO,11,END=506)NOVAR,YEAR,MON,TIT,(DATA(K),K=1,5)
C	WRITE(SIX,1023)NOVAR,YEAR,MON,TIT,(DATA(K),K=1,5)
11	FORMAT(I1,3A,5F11.3)
1023	FORMAT(1X,I1,3A,5F11.3, ' %WRITE(SIX,1023)')
C
	IF(MON .EQ. 'JUL')GO TO 509
	IF(MON .EQ. 'AUG')GO TO 509
	IF(MON .EQ. 'JUN')GO TO 509
	GO TO 9004
509	IF(MON .NE. ENDMONTH)THEN
		CLOSE (UNIT=TWO)
		WRITE(SIX,1009)MON,ENDMONTH
1009		FORMAT(' ROCOMP AND MASTER FILE MONTH DO NOT MATCH',/,
	1              ' MON .NE. ENDMONTH %WRITE(SIX,1009)',/,
	1	       ' MON=',A,' ENDMONTH=',A)
		GO TO 503
		END IF
	IF(TIT .NE. TITMAS)GO TO 504
C
	NOVAR=NOVAR+1
	TITLE=TIT
	REWIND TWO
C
	GO TO 507
C
C506	WRITE(SIX,1002)TITMAS
C1002	FORMAT(' COULD NOT FIND TITLE MATCH ',A,
C	1' IN MASTER FILE FRSTSUM.DAT  % 506 WRITE(SIX,1002)')
506	CLOSE (UNIT=2)
	GO TO 503
C
C
507	DO 300 K=1,5
C
	WRITE(ADATA(K),1003,ERR=9001,IOSTAT=IOS)DATA(K)
1003	FORMAT(F9.2)
C
300	CONTINUE
C	WRITE(SIX,1021)(DATA(K),K=1,5),(ADATA(K),K=1,5)
C1021	FORMAT(5X,'DATA= ',5F9.2,/,4X,'ADATA= ',5A)
C
C
	DO 301 K=1,NREC
C
	LOCATE=0
	IF(A(K)(1:20) .NE. TITLE)GO TO 301
	LOCATE=1
C
	DO 302 KK=K,NREC
C
	IF(A(KK)(111:114) .EQ. YEAR)GO TO 508
C
302	CONTINUE
C
	WRITE(SIX,1004)TITLE,YEAR
1004	FORMAT(1X,/,' COULD NOT FIND DATA FOR, ',A,1X,A,
	1'  %WRITE(SIX,1004)')
	CLOSE (UNIT=TWO)
C
	GO TO 503
C
508	WRITE(SIX,1005)IB,TITLE,A(KK)(1:60),A(KK)(111:114),
	1ENDMONTH,WY,IB
1005	FORMAT(126X,I6,/,1X,'FOUND ',A,1X,A,1X,A,' ROCOMP',A,'.',
	1A,20X,I6)
C
	ICHANGES=0
	B=A(KK)
C
	DO 304 I=1,POS-1
C
305	LOC=INDEX(VARIABLES(1:),CHANGES(I:I))
C	WRITE(SIX,1020)LOC,VARIABLES,CHANGES(I:I),POS
C1020	FORMAT(5X,' LOC= ',I2,5X,'VARIABLES= ',A,5X,'CHANGES= ',A1,
C	15X,'POS= ',I2 ' %WRITE(SIX,1020)')
	IF(LOC.EQ.0)GO TO 304
	START=LOC*9-9
	END=LOC*9+1
C
	READ(A(KK)(START+1:END-1),1003)RDATA(LOC)
	IF(LOC.EQ.1)THEN
		B=ADATA(1)//A(KK)(10:114)
		ICHANGES=ICHANGES+1
		A(KK)=B
	ELSE IF(LOC.GT.1)THEN
		B=A(KK)(1:START)//ADATA(LOC)//A(KK)(END:)
		ICHANGES=ICHANGES+1
		A(KK)=B
	END IF
	ERROR(ICHANGES)=0.
	IF(RDATA(LOC) .EQ. 0.)GO TO 510
	ERROR(ICHANGES)=ABS(RDATA(LOC)-DATA(LOC))/RDATA(LOC)*100.
C
510	VARIABLES(LOC:LOC)=' '
C
	GO TO 305
C
C
C
304	CONTINUE
C
C
C				
C	WRITE(SIX,1024)LOC,VARIABLES,CHANGES(I-1:I-1),POS
C1024	FORMAT(' LOC= ',I2,5X,'VARIABLES= ',A,5X,'CHANGES= ',A1,
C	15X,'POS= ',I2 ' %WRITE(SIX,1020)')
C
	IWR=1
	PER=0.
	DO 307 IK=1,ICHANGES
	IF(PER .GE. ERROR(IK))GO TO 307
	IF(ERROR(IK) .GE. 5.)THEN
			PER=ERROR(IK)
			IWR=2
			ENDIF
307	CONTINUE
C
C
	IPER=PER	
	IF(ICHANGES .GT. 0 .AND. IWR .EQ. 1)WRITE(SIX,1006)TITLE,B(1:60),
	1B(111:114),ICHANGES,IB
1006	FORMAT(7X,A,1X,A,1X,A,2X,I2,' CHANGES  %WRITE(SIX,1006)',3X,I6)
	IF(ICHANGES .GT. 0 .AND. IWR .EQ. 2)WRITE(SIX,1025)TITLE,B(1:45),
	1IPER,B(111:114),ICHANGES,IB
1025	FORMAT(7X,A,1X,A,3X,I3,'% CHANGE',2X,A,1X,I2,
	1' CHANGES  %WRITE(SIX,1006)',4X,I6)
	IF(ICHANGES.EQ.0)WRITE(SIX,1015)TITLE,B(1:60),B(111:114)
1015	FORMAT(7X,A,1X,A,1X,A,' NOT CHANGED  %WRITE(SIX,1015)')
C
C
	IB=IB+1
	CLOSE (UNIT=2)
	GO TO 503
C
301	CONTINUE
C
	IF(LOCATE .EQ. 0)WRITE(6,1007)TITLE
1007	FORMAT(' COULD NOT LOCATE ','-',A,'-','  %WRITE(6,1007)')	
	CLOSE (UNIT=2)
	GO TO 503
502	REWIND FOUR  ! START ANOTHER YEAR
C
400	CONTINUE
C
C
C
C
	OPEN(UNIT=THREE,CARRIAGECONTROL='LIST',STATUS='UNKNOWN')
	DO 303 K=1,NREC
	WRITE(THREE,1001)A(K)
303	CONTINUE
C
	GO TO 5000
C
C
C
C
C
C
C
C
9000	WRITE(SIX,1008)IOS,ENDMONTH,WY
1008	FORMAT(' ERROR ON OPEN UNIT=2, IOS= ',I2,
	1      'FILE= ROCOMP',A,'.',A,'  % 9000 WRITE(SIX,1008)')
C
	CLOSE (UNIT=2)
	GO TO 503
C
9001	WRITE(SIX,1011)(DATA(J),J=1,5)
1011	FORMAT(' ERROR ON INTERNAL WRITE',/,1X,'DATA= ',5F11.2,
	1'  % 9001 WRITE(SIX,1011)')
C
C
	GO TO 5000
C
9004	WRITE(SIX,1012)MON
1012	FORMAT(' THE MONTH WAS NOT JUNE, JULY, OR AUGUST MON= ',A,
	1'  % 9004 WRITE(SIX,1012)')
5000    CLOSE (UNIT=2)
	STOP
	END
